<?php
/**
 * Created by PhpStorm.
 * User: JYuans
 * Date: 2019/12/11
 * Time: 15:55
 */
namespace Bstage\Controller;

use Common\Model\CommonModel;

class TeamExamineRelationUpController extends BstageController {
    /**
     * 查询高层已设置考核人
     */
    public function getUpUserList(){
        $team_examine_id = $_REQUEST['team_examine_id'] ? $_REQUEST['team_examine_id'] : $this->errorReturn("缺少班子考评ID");

        $user_list = M("TeamExamineRelationUp")
            ->alias("ters")
            ->where(['team_examine_id'=>$team_examine_id])
            ->group("examine_user_id")
            ->join("__USER__ u on ters.examine_user_id = u.user_id")
            ->field("u.user_id,u.depart_id,u.name,u.rank,u.station")
            ->select();
        if(!$user_list){
            $this->returnJson(503,"无数据");
        }

        $depart_id_array = array_column($user_list,"depart_id");
        $depart_name_array = M("Department")
            ->where(['depart_id'=>['in',$depart_id_array]])
            ->getField("depart_id,name");

        foreach($user_list as $key=>$value){
            $user_list[$key]['depart_name'] = $depart_name_array[$value['depart_id']];

            $user_list[$key]['rank_str'] = CommonModel::rank_str_array[$value['rank']];
        }

        $this->successReturn("获取成功",['list_data'=>$user_list]);
    }

    /**
     * 修改高层考评设置
     */
    public function addUpRelation(){
        $team_examine_id = $_REQUEST['team_examine_id'] ? $_REQUEST['team_examine_id'] : $this->errorReturn("缺少班子考评ID");

        $team_examine_data = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("status")
            ->find();
        if($team_examine_data['status'] >=2){
            $this->errorReturn("考评已开始或已结束，无法修改");
        }

        $user_id_json = $_REQUEST['user_id_json'] ? $_REQUEST['user_id_json'] : $this->errorReturn("缺少高层人员ID_JSON");

        $user_id_array = json_decode($user_id_json,true);
        if(!is_array($user_id_array)){
            $this->errorReturn("高层人员ID_JSON错误");
        }

        if($user_id_array){
            $check_user_id = M("User")
                ->where(['user_id'=>['in',$user_id_array],'rank'=>5])
                ->count();
            if($check_user_id != count($user_id_array)){
                $this->errorReturn("人员ID中存在不是高层人员的ID");
            }
        }

        $model = M();
        $model->startTrans();

        if($user_id_array){
            //获取所有班子数据
            $team_list = M("Team")
                ->field("team_id")
                ->select();

            //获取已添加的关系
            $all_examine_user_id_array = M("TeamExamineRelationUp")
                ->where(['team_examine_id'=>$team_examine_id])
                ->group("examine_user_id")
                ->getField("examine_user_id",true);

            if($all_examine_user_id_array){
                //需要添加的关系
                $need_add_user_id_array = array_values(array_diff($user_id_array,$all_examine_user_id_array));
                for($i=0;$i<count($need_add_user_id_array);$i++){
                    foreach($team_list as $key=>$value){
                        $new_data = array();
                        $new_data['team_examine_id'] = $team_examine_id;
                        $new_data['be_examine_team_id'] = $value['team_id'];
                        $new_data['examine_user_id'] = $need_add_user_id_array[$i];

                        $add_relation_result = M('TeamExamineRelationUp')
                            ->add($new_data);
                        if(!$add_relation_result){
                            $model->rollback();
                            $this->errorReturn("添加关系失败，请重新尝试");
                        }
                    }
                }

                //需要删除的关系
                $need_delete_user_id_array = array_values(array_diff($all_examine_user_id_array,$user_id_array));
                if($need_delete_user_id_array){
                    $delete_relation_result = M("TeamExamineRelationUp")
                        ->where(['team_examine_id'=>$team_examine_id,'examine_user_id'=>['in',$need_delete_user_id_array]])
                        ->delete();
                    if(!$delete_relation_result){
                        $model->rollback();
                        $this->errorReturn("设置审核关系失败，请重新尝试");
                    }
                }
            }else{
                for($i=0;$i<count($user_id_array);$i++){
                    foreach($team_list as $key=>$value){
                        $new_data = array();
                        $new_data['team_examine_id'] = $team_examine_id;
                        $new_data['be_examine_team_id'] = $value['team_id'];
                        $new_data['examine_user_id'] = $user_id_array[$i];

                        $add_relation_result = M('TeamExamineRelationUp')
                            ->add($new_data);
                        if(!$add_relation_result){
                            $model->rollback();
                            $this->errorReturn("添加关系失败，请重新尝试");
                        }
                    }
                }
            }
        }else{
            //高层人员JSON为空，清空所有关系
            $relation_count = M("TeamExamineRelationUp")
                ->where(['team_examine_id'=>$team_examine_id])
                ->count();

            if($relation_count){
                $delete_relation_result = M("TeamExamineRelationUp")
                    ->where(['team_examine_id'=>$team_examine_id])
                    ->delete();
                if(!$delete_relation_result){
                    $model->rollback();
                    $this->errorReturn("修改设置失败，请重新尝试");
                }
            }else{
                $model->rollback();
                $this->errorReturn("请选择要设置的高层考评关系");
            }
        }

        $model->commit();
        $this->successReturn("设置成功");
    }

    /**
     * 删除高层考评设置
     */
    public function deleteUpRelation(){
        $team_examine_id = $_REQUEST['team_examine_id'] ? $_REQUEST['team_examine_id'] : $this->errorReturn("缺少班子考评ID");

        $user_id_json = $_REQUEST['user_id_json'] ? $_REQUEST['user_id_json'] : $this->errorReturn("缺少高层人员ID_JSON");

        $user_id_array = json_decode($user_id_json,true);
        if(!is_array($user_id_array)){
            $this->errorReturn("高层人员ID_JSON错误");
        }

        if(!$user_id_array){
            $this->errorReturn("请选择需要删除的高层人员");
        }

        $delete_relation_result = M("TeamExamineRelationUp")
            ->where(['team_examine_id'=>$team_examine_id,'examine_user_id'=>['in',$user_id_array]])
            ->delete();
        if(!$delete_relation_result){
            $this->errorReturn("删除关系失败，请重新尝试");
        }

        $this->successReturn("删除成功");

    }

    /**
     * 查询班子考评，高层人员考评情况列表
     */
    public function upExamineInfoList(){
        $team_examine_id = $_REQUEST['team_examine_id'] ? $_REQUEST['team_examine_id'] : $this->errorReturn("缺少班子考评ID");

        $up_examine_list = M("TeamExamineRelationUp")
            ->alias("ters")
            ->where(['team_examine_id'=>$team_examine_id])
            ->group("examine_user_id")
            ->join("__USER__ u on ters.examine_user_id = u.user_id")
            ->field("u.user_id,u.name,u.depart_id,u.rank,u.station,ters.comprehensive_status")
            ->select();
        if(!$up_examine_list){
            $this->returnJson(503,"无数据");
        }

        $depart_name_array = array();
        if($up_examine_list){
            $depart_id_array = array_column($up_examine_list,'depart_id');

            $depart_name_array = M("Department")
                ->where(['depart_id'=>['in',$depart_id_array]])
                ->getField("depart_id,name");
        }

        foreach($up_examine_list as $key=>$value){
            $up_examine_list[$key]['depart_name'] = isset($depart_name_array[$value['depart_id']])?$depart_name_array[$value['depart_id']]:"";

            $up_examine_list[$key]['rank'] = CommonModel::rank_str_array[$value['rank']];

            if($value['comprehensive_status'] == 1){
                $up_examine_list[$key]['comprehensive_status_str'] = "已提交";
            }else{
                $up_examine_list[$key]['comprehensive_status_str'] = "未提交";
            }
        }

        $this->successReturn("获取成功",$up_examine_list);
    }

    /**
     * 查询班子考评，高层考评详情
     */
    public function upExamineInfo(){
        $team_examine_id = $_REQUEST['new_team_examine_id'] ? $_REQUEST['new_team_examine_id'] : $this->errorReturn("缺少班子考评ID");

        $examine_user_id = $_REQUEST['examine_user_id'] ? $_REQUEST['examine_user_id'] : 0;

        $team_id = $_REQUEST['team_id'] ? $_REQUEST['team_id'] : 0;

        $team_examine_data = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("item_json")
            ->find();
        if(!$team_examine_data){
            $this->errorReturn("班子考评ID错误，请重新尝试");
        }

        $team_where = array();
        if($team_id){
            $team_where['team_id'] = $team_id;
        }
        $team_list = M("Team")
            ->where($team_where)
            ->getField("team_id,name,name_abb");

        if(!$team_list){
            $this->returnJson(503,"无数据1");
        }

        foreach($team_list as $key=>$value){
            $team_list[$key]['team_id'] = $key;
            $team_list[$key]['item_result'] = array();
            $team_list[$key]['amount_item_result'] = array();
            $team_list[$key]['amount_score'] = 0;
        }

        $team_examine_where = array();
        $team_examine_where['team_examine_id'] = $team_examine_id;
        if($team_id){
            $team_examine_where['be_examine_team_id'] = $team_id;
        }
        if($examine_user_id){
            $team_examine_where['examine_user_id'] = $examine_user_id;
        }
        $team_examine_where['comprehensive_status'] = 1;
        $team_examine_list = M("TeamExamineRelationUp")
            ->where($team_examine_where)
            ->field("be_examine_team_id,examine_user_id,item_result,item_score")
            ->select();

        if(!$team_examine_list){
            $this->returnJson(503,"无数据2");
        }

        $user_id_array = array_column($team_examine_list,'examine_user_id');

        $user_name_array = M("User")
            ->where(['user_id'=>['in',$user_id_array]])
            ->getField("user_id,name");

        $all_submit_count = count($user_name_array);
        foreach($team_examine_list as $key=>$value){
            $item_result = json_decode($value['item_result'],true);
            $new_data = array();
            $new_data['item_result'] = $item_result;
            $new_data['item_score'] = $value['item_score'];
            $new_data['examine_user_name'] = $user_name_array[$value['examine_user_id']];

            $team_list[$value['be_examine_team_id']]['item_result'][] = $new_data;
            if(!$team_list[$value['be_examine_team_id']]['amount_item_result']){
                $team_list[$value['be_examine_team_id']]['amount_item_result'] = $item_result;
            }else{
                for($i=0;$i<count($item_result);$i++){
                    $team_list[$value['be_examine_team_id']]['amount_item_result'][$i] = bcadd(
                        $team_list[$value['be_examine_team_id']]['amount_item_result'][$i],
                        $item_result[$i],2);
                }
            }
            $team_list[$value['be_examine_team_id']]['amount_score'] = bcadd($team_list[$value['be_examine_team_id']]['amount_score'],$value['item_score'],2);
        }

        $team_list = array_values($team_list);
        foreach($team_list as $key=>$value){
            for($i=0;$i<count($value['amount_item_result']);$i++){
                $team_list[$key]['amount_item_result'][$i] = bcdiv($team_list[$key]['amount_item_result'][$i],$all_submit_count,2);
            }
            $team_list[$key]['amount_score'] = bcdiv($value['amount_score'],$all_submit_count,2);
        }

        $return_data = array();
        $return_data['item_array'] = json_decode($team_examine_data['item_json']);
        $return_data['data_list'] = $team_list;
        $this->successReturn("获取成功",$return_data);
    }
}